﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TV_2013.Handle;
using TV_2013.Model;
using TV_2013.View.Feature2.DrawText;

namespace TV_2013.View.DrawText
{
    public partial class DrawText : PhoneApplicationPage
    {
        private Stroke _currentStroke;
        private string letter = null;

        public DrawText()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            letter = NavigationContext.QueryString["letter"].ToString();

            if ("null" == letter)
            {
                letter = null;
            }
            else
            {
                tbx_letter.Text = letter;
            }
        }

        private void inkTest_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (_currentStroke != null)
                _currentStroke.StylusPoints.Add(GetStylusPoint(e.GetPosition(inkTest)));  
        }

        private void inkTest_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            inkTest.CaptureMouse();
            _currentStroke = new Stroke();
            _currentStroke.StylusPoints.Add(GetStylusPoint(e.GetPosition(inkTest)));
            _currentStroke.DrawingAttributes.Color = Colors.Black;
            _currentStroke.DrawingAttributes.Width = 10;
            inkTest.Strokes.Add(_currentStroke);  
        }

        private void inkTest_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            _currentStroke = null; 
        }

        private StylusPoint GetStylusPoint(Point position)
        {
            return new StylusPoint(position.X, position.Y);
        }

        private void btn_clear_Click(object sender, RoutedEventArgs e)
        {
            inkTest.Strokes.Clear();
        }

        private void btn_chooseLetter_Click(object sender, RoutedEventArgs e)
        {
            string link = "/View/Feature2/DrawText/ChooseLetter.xaml?n=51";
            NavigationService.Navigate(new Uri(link, UriKind.RelativeOrAbsolute));
        }

        private void btn_New_Click(object sender, RoutedEventArgs e)
        {
            inkTest.Strokes.Clear();
            tbx_letter.Text = null;
        }

        private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            NavigationService.Navigate(new Uri("/View/Feature2/MainGameScreen.xaml", UriKind.RelativeOrAbsolute));
        }  
    }
}